package com.controller.ht;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.nutz.ioc.val.SysPropValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.common.ajax.AjaxRes;
import com.common.mybatis.Page;
import com.common.utils.AppMD5Util;
import com.common.utils.base.UuidUtil;
import com.controller.base.BaseController;
import com.entity.Appendix;
import com.entity.AppendixRelationship;
import com.entity.Login;
import com.entity.User;
import com.entity.UserIndustry;
import com.entity.UserIndustryRelationship;
import com.service.AppendixRelationshipService;
import com.service.AppendixService;
import com.service.LoginService;
import com.service.UserIndustryService;
import com.service.UserService;
import com.service.userIndustryRelationshipService;

@Controller
@RequestMapping("/user/")
public class UserController extends BaseController<Object>{

	@Autowired
	private UserIndustryService uiservice;
	@Autowired
	private UserService userservice;
	@Autowired
	private userIndustryRelationshipService uirservice;
	@Autowired
	private LoginService loginservice;
	@Autowired
	private AppendixRelationshipService apprpservice;
	/**
	 * 查询所有
	 * @return
	 */
	@RequestMapping("/findAll")
	@ResponseBody
	public AjaxRes findAll(UserIndustry ui){
		AjaxRes res = new AjaxRes();
		List<UserIndustry> uis = uiservice.find(ui);
		res.setSucceed(uis, "数据获取成功");
		return res;
	}
	/**
	 * 分页查询
	 * @return
	 */
	@RequestMapping("/findByPage")
	@ResponseBody
	public AjaxRes findByPage(User user,Page<User> p){
		AjaxRes res = new AjaxRes();
		Page<User> pageUis = userservice.findByPage(user, p);
		res.setSucceed(pageUis, "数据获取成功");
//		List<userIndustry> uis = pageUis.getResults();
//		mv.setViewName("pulicpage/index");
		return res;
	}
	/**
	 * 修改用户信息并返回修改结果
	 * @return
	 */
	@RequestMapping("/update")
	@ResponseBody
	public AjaxRes update(User user,UserIndustryRelationship uir,Login login,String apprpId){
		AjaxRes res=new AjaxRes(); 
		AppendixRelationship apprp = new AppendixRelationship();
		String pwd = AppMD5Util.MD5(login.getLoginPwd());
		login.setId(UuidUtil.get32UUID());
		login.setLastLoginTime(new Date());
		login.setPwdEncryption("MD5");
		login.setLoginIp("未知");
		login.setLoginPwd(pwd);
		if(user.getId().equals("0")){
			user.setId(UuidUtil.get32UUID());
			//用于测试是否添加成功的变量
			uir.setId(UuidUtil.get32UUID());
			uir.setUserClassNo(user.getUserCLassNo());
			uir.setUserId(user.getId());
			uir.setUserIndustry(user.getUserIndustry());
			user.setLoginNo(login.getId());
			if(!apprpId.equals("")){
				//添加附件关系
				apprp.setId(apprpId);
				List<AppendixRelationship> apprpUis = apprpservice.find(apprp);
				apprpUis.get(0).setRelationNo(user.getId());
				apprpUis.get(0).setRelationTName("user");
				apprpservice.update(apprpUis.get(0));
			}
			Integer a =	userservice.insert(user);
			Integer b = uirservice.insert(uir);
			loginservice.insert(login);
			if(a==0 && b==0){
				res.setResMsg("添加失败");
			}else{
				res.setResMsg("添加成功");
			}
		}else{
			uir.setUserId(user.getId());
			List<UserIndustryRelationship> uirUis = uirservice.find(uir);
			uir=uirUis.get(0);
			uir.setIndustryName(user.getIndustryName());
			uir.setUserClassNo(user.getUserCLassNo());
			uir.setUserIndustry(user.getUserIndustry());
			if(!apprpId.equals("")){
				//添加附件关系
				apprp.setId(apprpId);
				List<AppendixRelationship> apprpUis = apprpservice.find(apprp);
				apprpUis.get(0).setRelationNo(user.getId());
				apprpUis.get(0).setRelationTName("user");
				apprpservice.update(apprpUis.get(0));
			}
			//用于测试是否修改成功的变量
			Integer a =userservice.update(user);
			Integer b = uirservice.update(uir);
			if(a==0 && b==0){
				res.setResMsg("修改失败");
			}else{
				res.setResMsg("修改成功");
			}
		}
		return res;
	}
	@RequestMapping("/delect")
	@ResponseBody
	public AjaxRes delect(User user){
		AjaxRes res = new AjaxRes();
		//用于测试是否添加成功的变量
		Integer delRes = userservice.delete(user);
		if(delRes==0){
			res.setResMsg("删除失败");
		}else{
			res.setResMsg("删除成功");
		}
		return res;
	}

	//批量删除
	@RequestMapping("/deleteBatch")
	@ResponseBody
	public AjaxRes deleteBatch(String uiCheck){
		AjaxRes res=new AjaxRes(); 
		List uiList=new ArrayList();
		String[] getArr = uiCheck.split(",");
		for(int i=0;i<getArr.length;i++){
			if(i!=0){
				User user = new User();
				user.setId(getArr[i]);
				uiList.add(user);
			}
		}
		Integer a = userservice.deleteBatch(uiList);
		if(a==0){
			res.setResMsg("批量删除失败");
		}else{
			res.setResMsg("批量删除成功");
		}
		return res;
	}
	@RequestMapping("/userInfo")
	public String userInfo(Model model,String id){
		User user = new User();
		user.setId(id);
		if(!id.equals("0")){
			List<User> userList = userservice.find(user);
			if(userList.size()>0){
				user = userList.get(0);
			}
		}
		model.addAttribute("user", user);
		return "pages/userInfo";
	}
}
